package chenyanshuai;

import java.util.*;

public class Chenyanshuai {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] winningRedBalls = generateRedBalls();
        int winningBlueBall = generateBlueBall();
        int[] userRedBalls = new int[6];
        int userBlueBall;

        System.out.println("请输入6个红球号码（1-33，不能重复）：");
        for (int i = 0; i < 6; i++) {
            while (true) {
                System.out.print("红球 " + (i + 1) + "：");
                int number = scanner.nextInt();
                if (number >= 1 && number <= 33 && !contains(userRedBalls, number, i)) {
                    userRedBalls[i] = number;
                    break;
                } else {
                    System.out.println("输入错误或重复，请重新输入！");
                }
            }
        }

        System.out.print("请输入1个蓝球号码（1-16）：");
        while (true) {
            userBlueBall = scanner.nextInt();
            if (userBlueBall >= 1 && userBlueBall <= 16) {
                break;
            } else {
                System.out.println("蓝球号码输入错误，请重新输入！");
            }
        }

        int redMatchCount = countMatches(winningRedBalls, userRedBalls);
        boolean blueMatch = (winningBlueBall == userBlueBall);

        System.out.println("\n中奖号码为：" + Arrays.toString(winningRedBalls) + " + [" + winningBlueBall + "]");
        System.out.println("您的投注号码：" + Arrays.toString(userRedBalls) + " + [" + userBlueBall + "]");
        System.out.println("您命中 " + redMatchCount + " 个红球，" + (blueMatch ? "1" : "0") + " 个蓝球。");
        checkPrize(redMatchCount, blueMatch);
        scanner.close();
    }

    public static int[] generateRedBalls() {
        Set<Integer> redBalls = new HashSet<>();
        Random random = new Random();
        while (redBalls.size() < 6) {
            redBalls.add(random.nextInt(33) + 1);
        }
        int[] result = redBalls.stream().mapToInt(Integer::intValue).toArray();
        Arrays.sort(result);
        return result;
    }

    public static int generateBlueBall() {
        return new Random().nextInt(16) + 1;
    }

    public static boolean contains(int[] array, int num, int length) {
        for (int i = 0; i < length; i++) {
            if (array[i] == num) {
                return true;
            }
        }
        return false;
    }

    public static int countMatches(int[] winningBalls, int[] userBalls) {
        int count = 0;
        for (int num : userBalls) {
            for (int winNum : winningBalls) {
                if (num == winNum) {
                    count++;
                    break;
                }
            }
        }
        return count;
    }

    public static void checkPrize(int redMatch, boolean blueMatch) {
        if (redMatch == 6 && blueMatch) {
            System.out.println("恭喜！您中了一等奖（最高1000万）！");
        } else if (redMatch == 6) {
            System.out.println("恭喜！您中了二等奖（最高500万）！");
        } else if (redMatch == 5 && blueMatch) {
            System.out.println("恭喜！您中了三等奖（3000元）！");
        } else if (redMatch == 5) {
            System.out.println("恭喜！您中了四等奖（200元）！");
        } else if (redMatch == 4 && blueMatch) {
            System.out.println("恭喜！您中了四等奖（200元）！");
        } else if (redMatch == 4 || (redMatch == 3 && blueMatch)) {
            System.out.println("恭喜！您中了五等奖（10元）！");
        } else if (redMatch == 2 && blueMatch || blueMatch || redMatch == 1 && blueMatch) {
            System.out.println("恭喜！您中了六等奖（5元）！");
        } else {
            System.out.println("很遗憾，您未中奖！");
        }
    }
}
